import {reactive,toRaw} from "vue";
import {router} from '@/router/router';
function userLogin(){
    let loginInfo = reactive({
        userName:'',
        password:""
    })
    function justifyEmpty(loginInfo){
        if(loginInfo.password == ''){
            alert("请输入密码")
            return false
            
        }else if(loginInfo.userName == ''){
            alert("请输入用户民")
            return false
        }
        return true
    }
    //登录
    function submit(loginInfo){
        let loginInfoStore = JSON.parse(localStorage.getItem("loginInfo")) || {};
        if(loginInfoStore?.userName == loginInfo.userName && loginInfoStore?.password == loginInfo.password){
            console.log("store-success")
            localStorage.setItem("isLogin",true);
            router.push("/index")
        }else{
            emptyInput();
            alert("账号或者密码不对，请重新输入")
        }
    }
    //注册
    function register(loginInfo){
            const loginInfoT = JSON.stringify(toRaw(loginInfo));
            localStorage.setItem("loginInfo",loginInfoT)
            console.log("store-success")
            router.push({
                path:"/login"
            })
        
      }
      //清空表单
      function emptyInput(){
        loginInfo.userName = '';
        loginInfo.password = '';
      }
    return {
        loginInfo,
        justifyEmpty,
        submit,
        register
      }
}
export default userLogin